package copyengine.ui.component.list.old.cellrender
{
	import copyengine.ui.CESprite;
	import copyengine.ui.component.list.old.CEListCoreEvent;
	import copyengine.ui.component.list.old.animation.ICEListCellRenderAnimation;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.tick.GlobalTick;

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;

	public class CECellRenderSymbol extends CESprite implements ICEOldCellRender
	{
		/**
		 * the CEListCellRender anchor point is left-top point ,but for scale animation , the best anchor point is center
		 * so use an MovieClip(letf-top anchor point) warp  all asset, and do animation on the real cellRender
		 * but do cauclte on the warp class.
		 */
		private var cellRenderBgWarp:Sprite;
		protected var cellRenderBg:MovieClip;

		private var currentCellRenderIndex:int;

		protected var currentCellRenderData:ICECellRenderData;

		private var animation:ICEListCellRenderAnimation;

		private var isDrawNextTick:Boolean = false;

		private var eventParam:Object;

		public function CECellRenderSymbol()
		{
			super();
		}

		public final function initialize(_animation:ICEListCellRenderAnimation = null):void
		{
			eventParam = {};

			//in case child class can set some mc mouseEnable = false
			//ex: cellRender have an textField , we don't want use mouse over the textField then scale the cellRenderSize
			//then we need to 
			//1 set this.mouseEnabled = false;
			//2 set cellRenderBgWarp.mouseEnabled = false;
			//3 set cellRenderBg.mouseEnabled = false;
			//so in that case , in child class can set textFieild.mouseEnable = false.
			this.mouseEnabled = false;

			// warp the cellRender
			cellRenderBg = createCellRenderBg();
			cellRenderBg.mouseEnabled = true;

			cellRenderBgWarp = new Sprite();
			cellRenderBgWarp.addChild(cellRenderBg);
			cellRenderBg.x = cellRenderBg.width >> 1;
			cellRenderBg.y = cellRenderBg.height >> 1;
			cellRenderBgWarp.mouseEnabled = false;

			addChild(cellRenderBgWarp);
			//initialize
			doInitialze();

			addListener();

			animation = _animation;
			if (animation != null)
			{
				animation.target = cellRenderBg;
			}
		}

		public function setIsSelected(value:Boolean):void
		{
			onSelect(value);
		}

		protected final function addListener():void
		{
			GeneralUtils.addTargetEventListener(this, MouseEvent.MOUSE_DOWN, onClick, false, int.MIN_VALUE);
			GeneralUtils.addTargetEventListener(this, MouseEvent.ROLL_OVER, onRollOver);
			GeneralUtils.addTargetEventListener(this, MouseEvent.ROLL_OUT, onRollOut);
		}

		protected final function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(this, MouseEvent.MOUSE_DOWN, onClick);
			GeneralUtils.removeTargetEventListener(this, MouseEvent.ROLL_OVER, onRollOver);
			GeneralUtils.removeTargetEventListener(this, MouseEvent.ROLL_OUT, onRollOut);
		}

		public final function get container():DisplayObjectContainer
		{
			return this;
		}

		//========
		// Private
		//========
		public final function get cellIndex():int
		{
			return currentCellRenderIndex;
		}

		public final function set cellIndex(_value:int):void
		{
			currentCellRenderIndex = _value;
		}

		public final function setData(data:ICECellRenderData):void
		{
			currentCellRenderData = data;
			doSetRenderData();
		}

		public final function onRollOver(e:MouseEvent):void
		{
			animation && animation.onRollOver();
			doOnRollOver();
		}

		public final function onRollOut(e:MouseEvent):void
		{
			animation && animation.onRollOut();
			doOnRollOut();
		}

		public final function onSelect(_isSelected:Boolean):void
		{
			animation && animation.onSelect(_isSelected);
		}

		public final function onClick(e:MouseEvent):void
		{
			var event:CEListCoreEvent = new CEListCoreEvent(CEListCoreEvent.ITEM_CLICK);
			event.clickItemIndex = currentCellRenderIndex;
			event.clickItemData = currentCellRenderData;
			event.clickStageX = e.stageX;
			event.clickStageY = e.stageY;
			event.parameter = eventParam;
			container.dispatchEvent(event);

			eventParam = {};
		}

		/**
		 * <b>手动派发Change事件</b>
		 * </br></br>用于在非Click操作中对Cellrender进行更改,比如Cellrender中存在一个TF,
		 * 手动更改了TF的数字,此时所响应的键盘操作不会触发ItemClick
		 */
		protected final function dispatchListChangeEvent():void
		{
			var event:CEListCoreEvent = new CEListCoreEvent(CEListCoreEvent.ITEM_CHANGE);
			event.clickItemIndex = currentCellRenderIndex;
			event.clickItemData = currentCellRenderData;
			event.parameter = eventParam;
			container.dispatchEvent(event);

			eventParam = {};
		}


		public final function drawNow():void
		{
			if (!isDrawNextTick)
			{
				isDrawNextTick = true;
				GlobalTick.instance.callLaterAfterTickCount(startDrawNow);
			}
		}

		public final function recycle():void
		{
			cellRenderBg.scaleX = cellRenderBg.scaleY = 1;
			recycleCellRender();
			currentCellRenderData = null;
		}

		public final function cellRenderDispose():void
		{
			//Symbol CellRender 的 container 就是他自己
			//在调用GeneralUtils.removeTargetFromParent时候会调用dispose方法
		}

		override final protected function doDispose():void
		{
			//删除回调,因为有可能在CellRender被删除的前一帧待用DrawCellRender
			//此时cellrender已经dispose掉了
			GlobalTick.instance.removeTickNodeByFunction(startDrawNow);
			removeListener();
			disposeCellRender();
		}

		private function startDrawNow():void
		{
			isDrawNextTick = false;
			drawCellRender();
		}

		final protected function setEventParam(_key:String, _value:*):void
		{
			eventParam[_key] = _value;
		}

		//===========
		//==Overriable Function
		//===========
		protected function doInitialze():void
		{
		}

		protected function drawCellRender():void
		{
		}

		protected function recycleCellRender():void
		{
		}

		protected function disposeCellRender():void
		{
		}

		protected function doSetRenderData():void
		{
		}

		protected function doOnRollOver():void
		{
		}

		protected function doOnRollOut():void
		{
		}

		protected function createCellRenderBg():MovieClip
		{
			throw new Error("Child Class shoulde override this function")
		}


	}
}
